//
//  ConfigBean.swift
//  imprint
//
//  Created by 小柠檬🍋 on 2025/2/27.
//

import Foundation
class ConfigBean : ICloudTable, Identifiable {
    
    func doCustomExcluded() -> [String? : Any?] {
        return doExcluded(subject: self, excluded: [])
    }
    
    func getCloudTable() -> String {
        return "Config"
    }
    var objectId: String?
    public var aiChat = AiChatBean() //ai设置
    public var AiConfig = AiConfigBean()
    public var aiChatModel = AiChatModel()// ai模型
    public var moodConfig : [String:Int]? = nil //心情配置
    public var  aiChatModel1 : AiChatModel2? = nil//ai模型（豆包） 废弃
    public var  aiChatModel2 : AiChatModel2? = nil//ai模型（c）。  废弃
    public var aiChatModelMap : [String:AiModelCustomBean]? = nil  //新的ai模型
    public var aiChatModelMap2 : [String:AiModelCustomBean]? = nil  //备用模型
    public var aiError : [String:String]? = nil  //ai错误提醒
    private var aiDescribe2: AiDescribe? = nil//ai人设
    var aiTutorial: AiTutorialBean? = nil//ai教程
    public var switchConfig: SwitchConfig? = nil //开关
    public var serverConfig: ServerConfigBean? = nil//服务配置
    public var serverRequest: ServerRequestBean? = nil//服务请求
    public var reissueLetter : ReissueLetterBean? = nil//补发来信
    public var reissueLetter2 : ReissueLetterBean? = nil//补发来信
    public var examine : [String:String]? = nil
    public var appUpdate2 : AppUpdate? = nil
    public var notice : String? = nil
    var qqGroup:QqGroupBean? = nil
    public var emojiPicture:[String]? = nil//表情包
    
    func getAiDescribe() -> AiDescribe {
        return aiDescribe2 ?? AiDescribe()
    }
    
    //审核版本
    public func isExamineVersion() -> Bool {
        let value = examine?.first(where: {$0.key == "apple"})?.value ?? ""
        if value.isEmpty {
            return true
        }
        let versiton = String(getAppVersionAndBuild().version.stringValue.formatVersion())
        return value == versiton
    }
    
    
    func getSwitchConfig() -> SwitchConfig {
        return switchConfig ?? SwitchConfig()
    }
    
    func getServerConfig() -> ServerConfigBean {
        return serverConfig ?? ServerConfigBean()
    }
    
    //获取ai模型
    func getAiChatMode(key: String) -> AiModelCustomBean {
        if userManager.isLogin() {
            let model = userManager.getUser()?.getAiChatMode(key: key)
            if model != nil {
                return model!
            }
        }
        
        let value = aiChatModelMap?.first(where: {$0.key == key})?.value
        return value ?? AiModelCustomBean()
    }
    
    //备用模型
    func getAiChatMode2(key: String) -> AiModelCustomBean {
        let value = aiChatModelMap2?.first(where: {$0.key == key})?.value
        return value ?? AiModelCustomBean()
    }
    
    // 查找内容中包含的错误标识并返回对应信息
    func getAiError(content: String) -> String? {
        // 检查错误映射是否为空
        guard let errorMap = aiError, !errorMap.isEmpty else {
            return nil
        }
        
        // 遍历所有key，检查内容中是否包含该key
        for (key, value) in errorMap {
            if content.contains(key) {
                return value
            }
        }
        
        // 未找到匹配的错误标识
        return nil
    }
}
